package com.supcalls.collections;

import java.util.concurrent.*;

public class BlockingQueueExample {
    public static void main(String[] args) throws InterruptedException {
        // 创建线程安全的 BlockingQueue
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(100);

        // 创建生产者线程
        Thread producer = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                try {
                    queue.put(i);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        // 创建消费者线程
        Thread consumer = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                try {
                    Integer item = queue.take();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        });

        producer.start();
        consumer.start();

        producer.join();
        consumer.join();

        System.out.println("BlockingQueue size: " + queue.size());
    }
}

